package cn.fourhundredandeighteen.service.impl;

import cn.fourhundredandeighteen.dao.CourseDao;
import cn.fourhundredandeighteen.dao.StudentDao;
import cn.fourhundredandeighteen.entity.Student;
import cn.fourhundredandeighteen.service.StudentCourseService;
import cn.fourhundredandeighteen.service.StudentService;
import cn.fourhundredandeighteen.utill.MysqlConnect;
import cn.fourhundredandeighteen.view.vo.LoginVO;

import java.util.List;

/**
 * @description:
 * @author: liuyongqi
 * @time: 2020/12/15
 */
public class StudentServiceImpl implements StudentService {
    public String login(LoginVO loginVO) {
        StudentDao studentDao= MysqlConnect.into().getMapper(StudentDao.class);
        try {
            String student = studentDao.selectLogin(loginVO);
            MysqlConnect.destroy();
            return student;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int save(Student student) {
        StudentDao studentDao=MysqlConnect.into().getMapper(StudentDao.class);
        int i = studentDao.insertSelective(student);
        MysqlConnect.destroy();
        if (i>0){
            return i;
        }else
            return 0;
    }

    @Override
    public int delete(Long id) {
        StudentDao studentDao=MysqlConnect.into().getMapper(StudentDao.class);
        int i = studentDao.deleteByPrimaryKey(id);
        MysqlConnect.destroy();
        if (i>0)
            return i;
        else
            return 0;
    }

    @Override
    public Boolean studentSelectCourse(Long courseId, Long studentId) {
        StudentCourseService studentCourseService=new StudentCourseServiceImpl();
          int i = studentCourseService.saveStudentToCourse(studentId, courseId);
          if (i>0){
              return true;
          }else
              return false;
    }

    @Override
    public int upDataStudentInfo(Student student) {
        StudentDao studentDao=MysqlConnect.into().getMapper(StudentDao.class);
        try {
            int i = studentDao.updateByPrimaryKeySelective(student);
            MysqlConnect.destroy();
            return i;
        }catch (Exception e){
            return 0;
        }

    }

    @Override
    public List<Student> selectStudentList(List<Long> studentIds) {
        StudentDao studentDao=MysqlConnect.into().getMapper(StudentDao.class);
        List<Student> students = studentDao.selectStudentList(studentIds);
        MysqlConnect.destroy();
        if (students==null)
            return null;
        return students;
    }

    @Override
    public Student selectStudent(Long studetId) {
        StudentDao studentDao=MysqlConnect.into().getMapper(StudentDao.class);
        Student student = studentDao.selectByPrimaryKey(studetId);
        if (student ==null)
            return null;
        return student;
    }

    @Override
    public List<Student> selectAllStudent() {
        StudentDao studentDao=MysqlConnect.into().getMapper(StudentDao.class);
        List<Student> students = studentDao.selectAllStudent();
        if (students!=null){
            return students;
        }
        return null;
    }

}
